import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


import javax.microedition.midlet.MIDlet;
import javax.microedition.io.file.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

public class FileDemo extends MIDlet implements CommandListener {
		List listRoots, listContents;
		private String currentRoot, currentPath, writeFile, writeContents;
		private Command cmOK,cmExit, cmNew;
		private Form WaitForm;
		
		public void FileDemo() {
			}
		
		public void startApp() {
			WaitForm = new Form("Espere por favor...");
			WaitForm.setCommandListener(this);
			cmExit = new Command("Exit", Command.EXIT, 1);
			cmNew = new Command("New", Command.ITEM,2);
			SelectRoot();
		}
		
		public void pauseApp() {
		}
		
		public void destroyApp(boolean condition) {
			notifyDestroyed();
		}
		
		public void SelectRoot() {
			Enumeration rootListEnum = FileSystemRegistry.listRoots();
			listRoots = new List("Elija el root:", Choice.EXCLUSIVE);
			while (rootListEnum.hasMoreElements()) {
			  String rootName = (String) rootListEnum.nextElement();
			  listRoots.append(rootName, null);
			}
			cmOK = new Command("OK", Command.ITEM, 1);
			
			listRoots.addCommand(cmOK);
			listRoots.addCommand(cmExit);
			listRoots.setCommandListener(this);
			Display.getDisplay(this).setCurrent(listRoots);			
		}
		
		public void ShowContents() {
				listContents = new List("Contenidos de " + currentRoot, Choice.EXCLUSIVE);
				Display.getDisplay(this).setCurrent(WaitForm);
				Thread t = new Thread() {
					    public void run() {
						      try{
							
							        FileConnection myFc = (FileConnection)
							                              Connector.open("file:///" + currentRoot);
							        Enumeration fileEnum = myFc.list("*", false);
							        while(fileEnum.hasMoreElements()) {
							          String fileName = (String) fileEnum.nextElement();
							          listContents.append(fileName, null);
							        }
						      } catch (Throwable th) {
						    	  	System.out.println(th.toString());
						      }
					    }
				  };
				  t.start();
				listContents.addCommand(cmExit);
				listContents.addCommand(cmNew);
				listContents.setCommandListener(this);
				Display.getDisplay(this).setCurrent(listContents);
		
		}
		
		public void WriteToFile(String fileName, String contents) {
			writeFile = fileName;
			writeContents = contents;
			
			Thread t = new Thread() {
				  public void run() {
					    try{
						  
						      FileConnection conn = (FileConnection) Connector.open(writeFile, Connector.READ_WRITE);
						      if (!conn.exists()) {
						    	  conn.create();
						      }
						      OutputStream outSt = conn.openOutputStream();
						      PrintStream printSt = new PrintStream(outSt);
						      printSt.println(writeContents);
						      conn.close();
						 
					    } catch(IOException e) {
					    	System.out.println("IOException in WriteToFile: " + e.toString());
					    } catch(SecurityException e) {
					    	System.out.println("SecurityException in WriteToFile: " + e.toString());
					    }
				  }
			};
			t.start();
		}
		
		public void commandAction(Command cm, Displayable d) {
			//System.out.println("En commandAction");
			if (cm == cmOK && d == listRoots) {
				  System.out.println("Elegiste: " + listRoots.getString(listRoots.getSelectedIndex()));
				  currentRoot = listRoots.getString(listRoots.getSelectedIndex());
				  ShowContents();
			} else if (cm == cmExit) {
				destroyApp(false);
			} else if (cm == cmNew) {
				WriteToFile("file:///" + currentRoot + "newfile.txt","Sample, sample, sample");
			}
		}
}